package com.xzkingdee.demo.controller;


import com.xzkingdee.demo.model.Record;
import com.xzkingdee.demo.spider.SuperSpider;
import com.xzkingdee.demo.util.CommenUtil;
import com.xzkingdee.demo.util.TXTUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
public class DealController {

    @RequestMapping("hello")
    public String hello(){
        return "ok";
    }

    @RequestMapping("catchAndAnalysis")
    public String catchAndAnalysis(@RequestParam String url){
        SuperSpider superSpider = new SuperSpider(url);
        new Thread(() -> {
            Record record = new Record();
            record.setTime(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            record.setId(CommenUtil.randomUUID());
            record.setUrl(url);
            TXTUtil.writeFile(TXTUtil.WORKING, record);
            Spider.create(superSpider).addUrl(url).thread(10).run();
            superSpider.doAnalysis();


        }).start();

        return "1";
    }



}
